package summer.greedy;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class Solution738 {
    public int monotoneIncreasingDigits(int n) {
        char[] chars = String.valueOf(n).toCharArray();
        int index = chars.length;
        for (int i = chars.length - 1; i > 0; i--) {
            int cur = chars[i] - '0';
            int pre = chars[i - 1] - '0';
            if (cur < pre) {
                chars[i - 1] = (char) (pre - 1 + '0');
                index = i;
            }
        }
        for (int i = index; i < chars.length; i++) {
            chars[i] = '9';
        }
        return Integer.parseInt(String.valueOf(chars));
    }
}
